#ifndef SHAREDOBJECT_H
#define SHAREDOBJECT_H

#include <Globals.h>
#include <Resource.h>
#include <Process.h>
#include <Lockable.h>

namespace SharedObject_IF
{
  const int permit =0;
};

/**
  A buffer that can be shared across address spaces. The buffer can be write protected.
  Setting the preprocessor symbol SO_DEBUG causes any odd start/end sections to be preloaded with 
  a sentinel so the object can detect overflow on deallocation.

**/
class SharedObject : public Callable, public Lockable
{
public:
  /**
    This object is 'owned' by the calling process, and by default all other processes cannot read/write
    to it.
  **/
  SharedObject(Address length);
  
  /**
    Destroys a shared object, unmapping it from all address spaces. It does not ensure any non-linked pages
    are copied back into their source addresses. All changes to these are lost.
  **/
  ~SharedObject();
  
  /**
    The target process has permissions given/revoked on this object, and the object is made available
    in its address space.
  **/
  Address permit(u32int pid, bool write);
  
  /**
    We are a system resource.
  **/
  Resource *resource;
  
private:
  u32int owner;       // The process that owns this object.
  
  Address *frames;
  u32int nFrames;
  
  // Maps a process to the address the object is mapped in to.
  typedef Map<Process*, Address, 5> ProcessMap;
  ProcessMap mappedProcesses; // Which processes have us mapped in, and where.
};

#endif
